/*
   Copyright 2009-2010 the original author or authors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.googlecode.kohmori.reportgen;

import com.googlecode.kohmori.springmvc.DefaultReportParams;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;

public abstract class AbstractSQLSource {
  
    public abstract JAXBElement<?> createReportObject(Connection dbConn, 
            DefaultReportParams reportParams)
        throws SQLException;
    
    public abstract String getJAXBPackageList();

    protected static Date getRunDate(Connection dbConn)  
        throws SQLException {

        Date runDate = null;
        
        String sqlStatement = "select sysdate as run_date from dual";
        Statement stmt = dbConn.prepareStatement(sqlStatement);

        ResultSet rs = stmt.executeQuery(sqlStatement);
        if (rs.next()) {
            runDate = rs.getTimestamp("run_date");
        }
        rs.close();

        stmt.close();
        return runDate;       
    }
    
    
    protected String getASCIIStreamAsString(ResultSet rs, String columnName) 
            throws SQLException {
        InputStream inputStream = rs.getAsciiStream(columnName);
        StringBuffer stringBuffer = new StringBuffer("");
        
        byte[] byteBuffer = new byte[8132];
        int bytesRead;
        if (inputStream != null) {
            try {
                while ((bytesRead = inputStream.read(byteBuffer)) != -1) {
                    String chunk = new String(byteBuffer, 0, bytesRead);
                    stringBuffer.append(chunk);
                }
            } catch (IOException e) {
                throw new IllegalStateException(
                    "Application Error:  cannot read LONG field.");
            }
        }
        return stringBuffer.toString();
    }
    
    /* For debugging; does marshalling of input XML */
    protected void outputSourceXML(JAXBElement<?> jaxbElement) {
       try {
          JAXBContext jc = JAXBContext.newInstance(getJAXBPackageList());      
          Marshaller m = jc.createMarshaller();   
          m.marshal(jaxbElement, new FileOutputStream("XMLResults.xml"));
       } catch (Exception e) {
          System.out.println("JAXB Error: " + e.getMessage());
          e.printStackTrace(System.out);
       }
    }
}
